<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">
		<title>上课</title>
		<link rel="stylesheet" href="../css/mui.min.css">
		<script src="../js/mui.min.js"></script>
		<script src="../js/jquery.min.js"></script>
		<script src="../js/qrcode.min.js"></script>
		<style>
			#qrcode {
				margin-top: 15px;
				margin: 0 auto;
				padding-left: 15%;
				padding-right: 15%;
			}
		</style>
	</head>
	<body>
		<header class="mui-bar mui-bar-nav">
			<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
			<h1 class="mui-title">上课</h1>
			<span class="mui-icon mui-icon mui-icon-info mui-spin mui-pull-right" id="showSign"></span>
		</header>

		<!-- 内容框架 -->
		<div class="mui-content" style="margin: 10px;text-align: center;">
			<div class="mui-row" style="text-align: center;">
				<div id="title">上课</div>
			</div>
			<div id="qrcode"></div>
			<br>
			<div>
				<p>您可以访问以下网址展示二维码</p>
				<p id='web_url'></p>
			</div>
			<div style="text-align: center; margin-top:15px; margin: 0 auto;">
				<button class="mui-btn mui-btn-primary" onclick="end()">结束本次课程</button>
			</div>
		</div>


		<script>
			var cID;
			var description;
			var pass; //上课的六位密钥

			mui.plusReady(function() {
				cID = plus.webview.currentWebview().cID;
				description = plus.webview.currentWebview().description;
				init();
			});

			//初始化创建课程函数
			function init() {
				jQuery("#title").val(title);
				pass = randomString(6); //生成6位密钥
				var qrcode = new QRCode(document.getElementById("qrcode"), {});
				qrcode.makeCode(pass); //生成二维码
				mui.ajax({
					url: 'http://120.27.247.180:8080/School/courseCipher/startClass',
					type: "post",
					data: {
						"cID": cID,
						"description": description,
						"pass": pass
					},
					dataType: "jsonp",
					contentType: "application/x-www-form-urlencoded; charset=utf-8",
					jsonp: "jsoncallback",
					success: function(data) {
						data = JSON.parse(data);
						if (data.suc) {
							var web_url = jQuery("#web_url");
							web_url.text('http://120.27.247.180:8080/School/index.html?pwd=' + pass);
							mui.toast("创建课程成功");
						} else {
							mui.toast("创建课程失败");
						}
					},
					error: function() {
						mui.toast("服务器异常，请稍后再试");
					}
				});
			}

			//生成指定长度的随机字符串
			function randomString(len) {
				var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz123456789';
				var pwd = '';
				for (var i = 0; i < len; i++) {
					pwd += chars.charAt(Math.floor(Math.random() * chars.length));
				}
				return pwd;
			}

			//结束上课
			function end() {
				mui.ajax({
					url: 'http://120.27.247.180:8080/School/courseCipher/endClass',
					type: "post",
					data: {
						"cID": cID,
					},
					dataType: "jsonp",
					contentType: "application/x-www-form-urlencoded; charset=utf-8",
					jsonp: "jsoncallback",
					success: function(data) {
						mui.toast("已结束本次课程");
					},
					error: function() {
						mui.toast("服务器异常，请稍后再试");
					}
				});
				
			}

			

			//查看已经签到学生
			document.getElementById("showSign").addEventListener('tap', function(e) {
				mui.openWindow({
					url: 'member_sign.html',
					extras: {
						"cID": cID,
					}
				})
			});

			/* --------------------------数据填充----------------------------------*/
			//var qrcode = new QRCode(document.getElementById("qrcode"), {});
			//qrcode.makeCode("123456");
			/* --------------------------数据填充----------------------------------*/
		</script>
	</body>
</html>
